//
//  EditImageView.m
//  Estate
//
//  Created by houzhen on 2017/12/13.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import "EditImageView.h"

@implementation EditImageView
-(instancetype)initWithImage:(UIImage*)image{
    if(self = [super init]){
        self.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
        [self setView:image];
    }
    return self;
}
-(void)setView:(UIImage *)image{
    //设置图片名称
    self.backgroundColor = [UIColor blackColor];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, DEF_SCREEN_WIDTH, 20)];
    label.text = @"添加文字";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    
    //判断是否加水印
    BOOL water= [LSUtility isWaterMark];
    //需要打印水印
    if(water){
        NSArray *arr=[LSUtility queryPhotoWaterarkWord];
        //存在水印打印内容
        if(arr&&arr.count>0){
            if([arr containsObject:@"time"]||[arr containsObject:@"TIME"]){
                //DeLog(@"打水印");
                image=[UIImage imageWithimage:image content:[LSUtility nowTime] frame:CGRectZero];
            }
            
        }
    }
    self.imageEidtView = [[UIImageView alloc]initWithImage:image];
    self.imageEidtView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageEidtView.frame = CGRectMake(0, 0, self.mj_w, self.mj_h-40);
    [self addSubview:self.imageEidtView];
    
    self.bottomView = [[UITextView alloc]initWithFrame:CGRectMake(5, self.mj_h-40, self.mj_w-65, 40)];
    self.bottomView.textColor = [UIColor whiteColor];
    self.bottomView.layer.backgroundColor = [[UIColor clearColor] CGColor];
    self.bottomView.layer.borderColor = [[TTTheme defaultManger].defaultColor CGColor];
    self.bottomView.layer.borderWidth = 2.0;
    self.bottomView.layer.cornerRadius = 5;
    [self.bottomView.layer setMasksToBounds:YES];
    self.bottomView.delegate = self;
    self.bottomView.returnKeyType = UIReturnKeyDone;
    [self addSubview:self.bottomView];
    
    self.bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.bottomButton.frame = CGRectMake(self.mj_w-55, self.mj_h-40, 50, 40);
    self.bottomButton.layer.cornerRadius = 5;
    self.bottomButton.layer.masksToBounds = YES;
    [self addSubview:self.bottomButton];
    [self.bottomButton setTitle:@"完成" forState:UIControlStateNormal];
    self.bottomButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.bottomButton.titleLabel.textColor = [UIColor blackColor];
    self.bottomButton.backgroundColor = [TTTheme defaultManger].defaultColor;
    [self.bottomButton addTarget:self action:@selector(editImageDone) forControlEvents:UIControlEventTouchUpInside];
    [kKeyWindow addSubview:self];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    
    [self addGestureRecognizer:singleTap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    //注册键盘消失的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    //键盘高度
    CGRect keyBoardFrame = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.bottomView.frame =CGRectMake(5, self.mj_h-40- keyBoardFrame.size.height, self.mj_w-65, 40);
    self.bottomButton.frame = CGRectMake(self.mj_w-55, self.mj_h-40-keyBoardFrame.size.height, 50, 40);
}
-(void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    self.bottomView.frame =CGRectMake(5, self.mj_h-40, self.mj_w-65, 40);
    self.bottomButton.frame = CGRectMake(self.mj_w-55, self.mj_h-40, 50, 40);
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [self.bottomView resignFirstResponder];
        return NO;
    }
    return YES;
}
-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer
{
    [self endEditing:YES];
}
-(void)editImageDone{
    
    self.imageEidtView.image= [UIImage text:self.bottomView.text addToImage:self.imageEidtView.image];

    NSString *imageName=[NSString stringWithFormat:@"%@",[LSUtility getCurrentDateString]];
    //存入永久文件夹
    [LSUtility saveImagePermanent:self.imageEidtView.image withName:imageName];
    //存入首页临时文件夹
    [LSUtility saveImage:self.imageEidtView.image withName:[NSString stringWithFormat:@"%@",imageName]];
    //得到该照片路径
    NSString *path= [DEF_FILE_PATH stringByAppendingPathComponent:imageName];
    if (_completeBlock) {
        _completeBlock(path);
    }
    [self removeFromSuperview];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
